package test;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.*;

public class test1 {
	static void processEvent() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(
					"event.txt")));

			String line = br.readLine();
			while (line != null) {
				br.readLine();
				line = br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				line = br.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static void processInfo() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(
					"info.txt")));

			String line = br.readLine();
			while (line != null) {
				//get browser and os info
				String[] infos = getBrowOsInfo(br.readLine());
				System.out.println("os:"+infos[1]);
				if(infos[0].indexOf("MSIE") > -1){
					String[] browser = infos[0].split(" ");
					System.out.println("browser:"+browser[0]);
					System.out.println("version:"+browser[1]);
				}
				else if(infos[0].indexOf('/') > -1){
					System.out.println("browser:"+infos[0].substring(0,infos[0].indexOf('/')));
					System.out.println("version:"+infos[0].substring(infos[0].indexOf('/')+1,infos[0].length()));
				}
				else{
					System.out.println("browser:"+infos[0]);
					System.out.println("version:"+"-1.0");
				}
				
				String versionStr = br.readLine();
				
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				
				//get url info
				String url = br.readLine();
				System.out.println(url);
				if(url.indexOf('?') > -1){
					System.out.println("url:"+url.substring(0,url.indexOf('?')));
					System.out.println("paraStr:"+url.substring(url.indexOf('?')+1,url.length()));
				}
				else
					System.out.println("url:"+line);
				
				String dataStr = br.readLine();
				
				br.readLine();
				line = br.readLine();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static String[] getBrowOsInfo(String userAgentInfo){
		System.out.println(userAgentInfo);
		if(userAgentInfo.equals("null") || userAgentInfo.equals(""))
			return new String[]{"Unknown Browser", "Unknown OS"};
		String[] result = new String[2];
		String info = userAgentInfo.toUpperCase();  
        
		if(info.indexOf("(") > -1 && info.indexOf(")") > -1 && info.indexOf("(", info.indexOf(")")) > -1){
			info = info.substring(0, info.indexOf("(", info.indexOf(")")) - 1);
			System.out.println("info:"+info);
		}
		
		if(info.indexOf("(") == -1)
			return new String[]{"Unknown Browser", "Unknown OS"};
		
        String[] strInfo = info.substring(info.indexOf("(") + 1,  
                info.indexOf(")") - 1).split(";");  
        if ((info.indexOf("MSIE")) > -1) {  
            result[0] = strInfo[1].trim();  
            if(strInfo.length > 2)
            	result[1] = strInfo[2].trim();  
            else
            	result[1] = "Unknown OS";
        } else {  
            String[] str = info.split(" ");  
            if (info.indexOf("NAVIGATOR") < 0 && info.indexOf("FIREFOX") > -1) {  
            	result[0] = str[str.length - 1].trim(); 
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if ((info.indexOf("OPERA")) > -1) {  
            	result[0] = str[0].trim();  
            	result[1] = strInfo[0].trim();  
            } else if (info.indexOf("CHROME") < 0  
                    && info.indexOf("SAFARI") > -1) {  
            	result[0] = str[str.length - 1].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if (info.indexOf("CHROME") > -1) {  
            	result[0] = str[str.length - 2].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if (info.indexOf("NAVIGATOR") > -1) {  
            	result[0] = str[str.length - 1].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else if (info.indexOf("APPLEWEBKIT") > -1) {  
            	result[0] = str[str.length - 1].trim();  
            	if(strInfo.length > 2)
            		result[1] = strInfo[2].trim(); 
            	else
            		result[1] = "Unknown OS";  
            } else {  
            	result[0] = "Unknown Browser";  
            	result[1] = "Unknown OS";  
            }  
        }  
        System.out.println(result[0]+" "+result[1]);
        return result;
	}
	
	static void getParas(String paraStr){
		String[] paras = paraStr.split("&");
		for(String para : paras){
			if(para.indexOf('=') > -1){
				String[] keyValue = para.split("=");
				if(keyValue.length == 0)
					continue;
				System.out.println("paraKey:"+keyValue[0]);
				if(keyValue.length > 1)
					System.out.println("keyValue:"+keyValue[1]);
				else
					System.out.println("keyValue:"+null);
				
			}
		}
		
	}
	
	public static void main(String[] args) {
		//processEvent();
		processInfo();
	}
}
